package com.ruoyi.media.accountGroup.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.media.accountGroup.domain.bo.MediaAccountGroupBo;
import com.ruoyi.media.accountGroup.domain.vo.MediaAccountGroupVo;
import com.ruoyi.media.accountGroup.service.IMediaAccountGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 账号分组
 *
 * @author dqw
 * @date 2023-05-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/media/accountGroup")
public class MediaAccountGroupController extends BaseController {

    private final IMediaAccountGroupService iMediaAccountGroupService;

    /**
     * 查询带账号的分组列表
     */
//    @SaCheckPermission("media:accountGroup:list")
    @GetMapping("/list")
    public TableDataInfo<MediaAccountGroupVo> list(MediaAccountGroupBo bo, PageQuery pageQuery) {
        return iMediaAccountGroupService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询账号分组列表
     */
//    @SaCheckPermission("media:accountGroup:list")
    @GetMapping("/dataList")
    public TableDataInfo<MediaAccountGroupVo> dataList(MediaAccountGroupBo bo, PageQuery pageQuery) {
        return iMediaAccountGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出账号分组列表
     */
    @SaCheckPermission("media:accountGroup:export")
    @Log(title = "账号分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MediaAccountGroupBo bo, HttpServletResponse response) {
        List<MediaAccountGroupVo> list = iMediaAccountGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "账号分组", MediaAccountGroupVo.class, response);
    }

    /**
     * 获取账号分组详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("media:accountGroup:query")
    @GetMapping("/{id}")
    public R<MediaAccountGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iMediaAccountGroupService.queryById(id));
    }

    /**
     * 创建账号对应分组
     */
//    @SaCheckPermission("media:accountGroup:add")
    @Log(title = "账号分组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<?> add(@Validated(AddGroup.class) @RequestBody MediaAccountGroupBo bo) {
        return R.ok(iMediaAccountGroupService.insertByBo(bo));
    }

    /**
     * 修改账号分组
     */
//    @SaCheckPermission("media:accountGroup:edit")
    @Log(title = "账号分组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<?> edit(@Validated(EditGroup.class) @RequestBody MediaAccountGroupBo bo) {
        return R.ok(iMediaAccountGroupService.updateByBo(bo));
    }

    /**
     * 删除账号分组
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("media:accountGroup:remove")
    @Log(title = "账号分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iMediaAccountGroupService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


}
